unit UEmpresaPermissoes;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ExtCtrls, Db, DBTables, DBCtrls, Grids, DBGrids,
  RxLookup,UFomCadastro, Mask, CheckLst,Menus, DBClient, Provider, ComCtrls;

type
  TFEmpresaPermissao = class(TFormCadastro)
    Label1: TLabel;
    DBLEmpresa: TDBLookupComboBox;
    GBPermissoes: TGroupBox;
    CkLBPermissao: TCheckListBox;
    QEmpresaPermissao: TQuery;
    DSEmpresaPermissao: TDataSource;
    cdsCadastroEMP_CODIGO: TIntegerField;
    cdsCadastroEMP_RAZAO: TStringField;
    cdsCadastroEMP_FANTASIA: TStringField;
    cdsCadastroEMP_CNPJ: TStringField;
    cdsCadastroEMP_ENDERECO: TStringField;
    cdsCadastroEMP_BAIRRO: TStringField;
    cdsCadastroEMP_CEP: TStringField;
    cdsCadastroEMP_CIDADE: TStringField;
    cdsCadastroEMP_ESTADO: TStringField;
    cdsCadastroEMP_FONE: TStringField;
    cdsCadastroEMP_FAX: TStringField;
    cdsCadastroEMP_EMAIL: TStringField;
    cdsCadastroEMP_INSCESTADUAL: TStringField;
    cdsCadastroEMP_INSCMUNICIPAL: TStringField;
    cdsCadastroEMP_RESPONSAVEL: TStringField;
    cdsCadastroEMP_USACENTROCUSTO: TStringField;
    cdsCadastroEMP_AUTORIZABAIXA: TStringField;
    cdsCadastroNUMERONOTAFISCALENTRADA: TIntegerField;
    cdsCadastroSERIENOTAFISCALENTRADA: TStringField;
    cdsCadastroNUMERONOTAFISCALSAIDA: TIntegerField;
    cdsCadastroSERIENOTAFISCALSAIDA: TStringField;
    cdsCadastroNUMEROSELO: TIntegerField;
    cdsCadastroSERIESELO: TStringField;
    cdsCadastroEMP_LAYOUT: TStringField;
    cdsCadastroEMP_CONTACAIXA: TIntegerField;
    cdsCadastroEMP_DATA_ULT_ATU_BASE: TDateTimeField;
    cdsCadastroUSUARIO_CADASTRO: TStringField;
    cdsCadastroUSUARIO_AUTERACAO: TStringField;
    cdsCadastroUSUARIO_EXCLUSAO: TStringField;
    cdsCadastroDATA_CADASTRO: TDateTimeField;
    cdsCadastroDATA_EXCLUSAO: TDateTimeField;
    cdsCadastroDATA_AUTERACAO: TDateTimeField;
    cdsCadastroEXCLUIDO: TStringField;
    CbMarcarTodos: TCheckBox;
    procedure FormCreate(Sender: TObject);
    procedure BtnEditarClick(Sender: TObject);
    procedure BtnSalvarClick(Sender: TObject);
    procedure BtnCancelarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure BtnIncluirClick(Sender: TObject);
    procedure BtnExcluirClick(Sender: TObject);

    Procedure Cria_Arvore(Cod_Emp: String);
    procedure DSEmpresaPermissaoDataChange(Sender: TObject; Field: TField);

    Function Pega_Codigo_Permissao(Nome: String) : Integer;

    Procedure Carrega_Permissoes(Cod_Emp: String);
    procedure CbMarcarTodosClick(Sender: TObject);
    procedure DSEmpresaPermissaoStateChange(Sender: TObject);
    procedure dsCadastroDataChange(Sender: TObject; Field: TField);

  private
    { Private declarations }

  public
    { Public declarations }
  end;

Const Verifica_Permissao_Empresa = ' SELECT * FROM EMPRESAPERMISSAO                            '+
                                   ' WHERE (EMP_PER_EMPRESA = %s) AND (EMP_PER_PERMISSAO = %s) ';

var
  FEmpresaPermissao : TFEmpresaPermissao;
  Senha             : String;
  Codigos_Menus     : TStringlist;
implementation

uses UGeral, UDmGestao;

{$R *.DFM}
procedure TFEmpresaPermissao.FormCreate(Sender: TObject);
const
 Sql_Parametro = ' SELECT * FROM PARAMETRO WHERE CODIGOEMPRESA = %s ';
 Sql_EMPRESA = ' SELECT * FROM EMPRESA WHERE EMP_CODIGO = %s ';
begin

  QCadastro.SQL.Clear;
  QCadastro.SQL.Add('SELECT * FROM EMPRESA');

{  try
   QEmpresaPermissao.Close;
   QEmpresaPermissao.SQL.Clear;
   QEmpresaPermissao.SQL.Add(format(Sql_EMPRESA,[IntToStr(Codigoempresa)]));
   QEmpresaPermissao.Open;
   cdsCadastro.Close;
   cdsCadastro.Open;
   pcCadastro.ActivePage := TsCadastro;
  Except
   exit;
  end;}

  inherited;

  DSEmpresaPermissao.DataSet.Open;
  Cria_Arvore(DSEmpresaPermissao.DataSet.Fields[0].asstring);
  {
  FEmpresaPermissao.Constraints.MaxWidth  := 514;
  FEmpresaPermissao.Constraints.MinWidth  := 514;
  FEmpresaPermissao.Constraints.MaxHeight := 557;
  FEmpresaPermissao.Constraints.MinHeight := 557;
  }
  pcCadastro.ActivePage := TsCadastro;

end;

procedure TFEmpresaPermissao.BtnEditarClick(Sender: TObject);
begin
 inherited;
 DSEmpresaPermissao.DataSet.Edit;
 DBLEmpresa.Enabled := false;
end;

procedure TFEmpresaPermissao.BtnSalvarClick(Sender: TObject);
Const Deleta_Permissoes = ' DELETE FROM EMPRESAPERMISSAO WHERE EMP_PER_EMPRESA = %s ';
      Inseri_Permissao  = ' INSERT INTO EMPRESAPERMISSAO (EMP_PER_EMPRESA,EMP_PER_PERMISSAO) VALUES (%s,%s) ';

var Cod_Emp : String;
    I : Integer;
begin

  Cod_Emp := DblEmpresa.KeyValue;

  if Trim(Cod_Emp) = '' then
    Exit;

  executaSQL(Format(Deleta_Permissoes,[Cod_Emp]));

  for I := 0 to Codigos_Menus.Count - 1 do
  begin

    if CkLBPermissao.Checked[I] = True then
      executaSQL(Format(Inseri_Permissao,[Cod_Emp,Codigos_Menus[I]]));

    Update;

  end;

  DSEmpresaPermissao.DataSet.Post;

  inherited;

end;

procedure TFEmpresaPermissao.BtnCancelarClick(Sender: TObject);
begin
  inherited;
  DSEmpresaPermissao.DataSet.cancel;
end;

procedure TFEmpresaPermissao.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  Codigos_Menus := nil;

  FEmpresaPermissao := nil;
  Action := CaFree;    
  
  inherited;

end;

procedure TFEmpresaPermissao.BtnIncluirClick(Sender: TObject);
begin
  inherited;
  DSEmpresaPermissao.DataSet.Insert;
end;

procedure TFEmpresaPermissao.BtnExcluirClick(Sender: TObject);
begin
  inherited;
  DSEmpresaPermissao.DataSet.delete;
end;

procedure TFEmpresaPermissao.Cria_Arvore(Cod_Emp: String);
Var A,B,C,D,E,F,G,Cont : Integer;
    Nome_Menu,Caption_Menu : String;
begin

  if Trim(cod_Emp) = '' then
    Cod_Emp := '0';

  CkLBPermissao.Items.Clear;

  Codigos_Menus := nil;

  Codigos_Menus := TStringList.Create;

  Cont := -1;

  For A := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items.Count - 1 do
  Begin

    Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Caption,'&','',[]);
    Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Name;

    if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
    begin

      Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

      CkLBPermissao.Items.Add(Caption_Menu);
      Inc(Cont);

      If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
        CkLBPermissao.Checked[Cont] := true
      else
        CkLBPermissao.Checked[Cont] := False;

    end;

    For B := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Count - 1 do
    Begin

      Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Caption,'&','',[]);
      Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Name;

      if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
      begin

        Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

        CkLBPermissao.Items.Add('   '+Caption_Menu);
        Inc(Cont);

        If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
          CkLBPermissao.Checked[Cont] := true
        else
          CkLBPermissao.Checked[Cont] := False;

      end;

      For C := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Count - 1 do
      Begin

        Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Caption,'&','',[]);
        Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Name;

        if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
        begin

          Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

          CkLBPermissao.Items.Add('      '+Caption_Menu);
          Inc(Cont);

          If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
            CkLBPermissao.Checked[Cont] := true
          else
            CkLBPermissao.Checked[Cont] := False;

        end;

        For D := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Count - 1 do
        Begin

          Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Caption,'&','',[]);
          Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Name;

          if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
          begin

            Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

            CkLBPermissao.Items.Add('         '+Caption_Menu);
            Inc(Cont);

            If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
              CkLBPermissao.Checked[Cont] := true
            else
              CkLBPermissao.Checked[Cont] := False;

          end;

          For E := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Count - 1 do
          Begin

            Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Caption,'&','',[]);
            Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Name;

            if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
            begin

              Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

              CkLBPermissao.Items.Add('            '+Caption_Menu);
              Inc(Cont);

              If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                CkLBPermissao.Checked[Cont] := true
              else
                CkLBPermissao.Checked[Cont] := False;

            end;

            For F := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Count - 1 do
            Begin

              Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Caption,'&','',[]);
              Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Name;

              if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
              begin

                Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

                CkLBPermissao.Items.Add('               '+Caption_Menu);
                Inc(Cont);

                If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                  CkLBPermissao.Checked[Cont] := true
                else
                  CkLBPermissao.Checked[Cont] := False;

              end;

              For G := 0 to TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Count - 1 do
              Begin

                Caption_Menu := StringReplace(TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Items[G].Caption,'&','',[]);
                Nome_Menu    := TMainMenu(Application.MainForm.FindComponent('MainMenu1')).Items[A].Items[B].Items[C].Items[D].Items[E].Items[F].Items[G].Name;

                if (Caption_Menu <> '-') and not(Procura_Caracter(Caption_Menu,'*')) then
                begin

                  Codigos_Menus.Add(IntToStr(Pega_Codigo_Permissao(Nome_Menu)));

                  CkLBPermissao.Items.Add('                  '+Caption_Menu);
                  Inc(Cont);

                  If Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,IntToStr(Pega_Codigo_Permissao(Nome_Menu))])) then
                    CkLBPermissao.Checked[Cont] := true
                  else
                    CkLBPermissao.Checked[Cont] := False;

                end;

              end; // FOR G

            end; // FOR F

          end; // FOR E

        end; // FOR D

      end; // FOR C

    end; // FOR B

  end; // FOR A

 // Showmessage(Nomes_Menus.Strings[1]); 

end;

procedure TFEmpresaPermissao.DSEmpresaPermissaoDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;

    If DSEmpresaPermissao.DataSet.State in [dsbrowse] then
    begin

      DblEMpresa.KeyValue := DSEmpresaPermissao.DataSet.Fields[0].AsVariant;

      Update;

      Carrega_Permissoes(DblEMpresa.KeyValue);

    end;
    
end;

function TFEmpresaPermissao.Pega_Codigo_Permissao(Nome: String) : Integer;
Const Seleciona_Permissao = ' SELECT * FROM PERMISSAO WHERE PER_TRANSACAO = %s ';
begin

  Usando_Query_Pesquisa(Format(Seleciona_Permissao,[QuotedStr(Nome)]));

  Result := Query_Pesquisa.Fieldbyname('PER_CODIGO').AsInteger;

end;

procedure TFEmpresaPermissao.Carrega_Permissoes(Cod_Emp: String);
var I : Integer;
begin

  if Codigos_Menus <> nil then
  begin

    for I := 0 to Codigos_Menus.Count - 1 do
    begin

      if Usando_Query_Pesquisa(Format(Verifica_Permissao_Empresa,[Cod_Emp,Codigos_Menus.Strings[I]])) then
        CkLBPermissao.Checked[I] := True
      else
        CkLBPermissao.Checked[I] := False;

      Update;

    end;

  end;
  
end;

procedure TFEmpresaPermissao.CbMarcarTodosClick(Sender: TObject);
var I : Integer;
begin
  inherited;

  for I := 0 to Codigos_Menus.Count - 1 do
    CkLBPermissao.Checked[I] := CbMarcarTodos.Checked;

end;

procedure TFEmpresaPermissao.DSEmpresaPermissaoStateChange(
  Sender: TObject);
begin
  inherited;

  GBPermissoes.Enabled := not(DSEmpresaPermissao.DataSet.State in [DsBrowse]);

end;

procedure TFEmpresaPermissao.dsCadastroDataChange(Sender: TObject;
  Field: TField);
begin
  inherited;
  if dsCadastro.DataSet.State = dsBrowse then
  begin

    DBLEmpresa.KeyValue := dsCadastro.DataSet.FieldByName(DBLEmpresa.KeyField).AsInteger;

    Update;

    Carrega_Permissoes(DblEMpresa.KeyValue);

    end;
end;

end.
